﻿#include "SettleAccount.h"
#include "ui_settleaccount.h"

SettleAccount::SettleAccount(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::SettleAccount)
{
    ui->setupUi(this);
    time = new QLabel(ui->frameBottom);
    time->setGeometry(10, 10, 200, 40);
    time->setStyleSheet("QLabel{font: 20px; color: rgb(25, 25, 112);}");
    signal = new QLabel(ui->frameBottom);
    signal->setGeometry(700, 18, 41, 22);
    signal->setStyleSheet("QLabel{background-color:transparent;}");


    QDir dir(IMAGE_PATH);
    if(dir.exists())
    {
        ui->frameTop->setStyleSheet(LOGO_STY);
    }

#if (GUN_AMOUNT == 1)
    ui->pushButton->hide();
#endif
#if((PRODUCER == 3 || PRODUCER == 5) && SHOW_EXPENSE == 0)
    ui->ChargingServiceLabel->hide();
    ui->ChargingServiceEdit->hide();
    ui->Yuan_2->hide();
    ui->ChargingChargeLabel->hide();
    ui->MoneyEdit->hide();
    ui->Yuan->hide();
    ui->ChargingCustomAllMoneyLabel->hide();
    ui->ChargingCustomAllMoneyEdit->hide();
    ui->Yuan_3->hide();
#endif
    for(int i=0; i<GUN_AMOUNT; i++)
    {
        countdown[i] = new CountDownTime(ui->frameMid);
        countdown[i]->setGeometry(710, 10, 30, 40);
        countdown[i]->mysetText("30");
        UpDateSettle[i] = new QTimer(this);
        connect(this->countdown[i], SIGNAL(CoundDownTimeisOver()), this, SLOT(countdownOver()));
        connect(UpDateSettle[i], SIGNAL(timeout()), this, SLOT(UpdateSettleSlot()));
    }
    connect(stackLayout, SIGNAL(currentChanged(int)), this, SLOT(setCountHide(int)));
    connect(stackLayout, SIGNAL(currentChanged(int)), this, SLOT(ShowSettleInfo(int)));

}

void SettleAccount::setCountHide(int current)
{
    if(current == settleAccountPage)
    {
        ui->_focus_gun_->setText(QString::number(focus_gun_));

        protocol_exit_seek_card_status();//从充电界面跳转过来需要退出一下寻卡
        for(int i=0; i<GUN_AMOUNT; i++)
        {
            if(focus_gun_-1 != i)
            {
                countdown[i]->hide();
            }
            else
            {
                countdown[i]->show();
            }
        }
    }
}

void SettleAccount::ShowSettleInfo(int current)
{
    if(current == settleAccountPage)
    {
        char tmp[64] = {0};

//        ui->labelTitleStatus->setText(QString(tr("%l号枪结算状态")).arg(focus_gun_));
        sprintf(tmp,"%d号枪结算状态",focus_gun_);
        ui->labelTitleStatus->setText(tmp);

//        ui->ChargingTimeHourEdit->setText(QString::number(St[focus_gun_-1].hour));
        sprintf(tmp,"%d",St[focus_gun_-1].hour);
        ui->ChargingTimeHourEdit->setText(tmp);

//        ui->ChargingTimeMinEdit->setText(QString::number(St[focus_gun_-1].min));
        sprintf(tmp,"%d",St[focus_gun_-1].min);
        ui->ChargingTimeMinEdit->setText(tmp);

//        ui->ChargingTimeSecondEdit->setText(QString::number(St[focus_gun_-1].second));
        sprintf(tmp,"%d",St[focus_gun_-1].second);
        ui->ChargingTimeSecondEdit->setText(tmp);

//        ui->SOCEdit->setText(QString::number((St[focus_gun_-1].SOC), 'f', 0));
        sprintf(tmp,"%d",St[focus_gun_-1].SOC);
        ui->SOCEdit->setText(tmp);

//        ui->MoneyEdit->setText(QString::number((St[focus_gun_-1].chargemoney * 1.0) / 100, 'f', 2));//充电电费
        sprintf(tmp,"%.2f",St[focus_gun_-1].chargemoney*1.0/100);
        ui->MoneyEdit->setText(tmp);

#if (PRODUCER == 3)
        ui->ChargingCapacityEdit->setText(QString::number((St[focus_gun_-1].chargepower * 1.0) / 1000, 'f', 3));//充电电量
#else
//        ui->ChargingCapacityEdit->setText(QString::number((St[focus_gun_-1].chargepower * 1.0) / 1000, 'f', 2));//充电电量
        sprintf(tmp,"%.3f",St[focus_gun_-1].chargepower*1.0/1000);
        ui->ChargingCapacityEdit->setText(tmp);
#endif
//        ui->ChargingServiceEdit->setText(QString::number((St[focus_gun_-1].servermoney * 1.0) / 100, 'f', 2));//充电服务费
        sprintf(tmp,"%.2f",St[focus_gun_-1].servermoney*1.0/100);
        ui->ChargingServiceEdit->setText(tmp);

//        ui->ChargingCustomAllMoneyEdit->setText(QString::number((St[focus_gun_-1].allmoney * 1.0) / 100, 'f', 2));
        sprintf(tmp,"%.2f",St[focus_gun_-1].allmoney*1.0/100);
        ui->ChargingCustomAllMoneyEdit->setText(tmp);

        SetStopReason(St[focus_gun_-1].stopreason, St[focus_gun_-1].stopDetailReason);
    }
}
void SettleAccount::UpdateSettleSlot()
{
    QTimer* sender_ = static_cast<QTimer*>(sender());
    for(int i=0; i<GUN_AMOUNT; i++)
    {
        if(sender_ == UpDateSettle[i])//表示第i支枪发出的刷新超时信号
        {
            if((chargingData->gunmsg[i].charge_state != 1))//脱离正在充电状态
            {
                memset(St+i, 0, sizeof(struct SettleSt));
                //1填充结构体数据
                if (fulling_Info(i) >= 0)
                {
                    //2启动结算成功枪支倒计时
                    countdown[i]->CountDownStart();
                    if (focus_gun_ != i + 1)    // fouce on the end charging gun!
                    {
                        focus_gun_ = i + 1;
                    }
                    GUN_STATE[i].Page = settleAccountPage;
                    stackLayout->setCurrentIndex(settleAccountPage);
                    UpDateSettle[i]->stop();
                    chargingData->cmd4[i].req_state = 0;
                    chargingData->cmd2[i].result = 0;
                    Refreash_Thread[i].charge_status = 0;
                }
            }
        }
    }
}
void SettleAccount::SetStopReason(int stopreson, int detailReason)
{
    QString stopReasonValue;
    if( stopreson != 0)
    {
        switch(stopreson)
        {
            case 1:
                ui->labelStopReason->setText(tr("本地刷卡停止"));
                if(GUN_STATE[focus_gun_ - 1].VINflag == 1)
                    ui->labelStopReason->setText(tr("VIN停止"));
                break;
            case 2:
                ui->labelStopReason->setText(tr("远程终止充电"));
                break;
            case 3:
                ui->labelStopReason->setText(tr("充满自动停止"));
                break;
            case 4:
                ui->labelStopReason->setText(tr("余额不足"));
                break;
            case 5:
                //stopReasonValue = tr("充电桩故障-");
        //        stopReasonValue += QString::number(chargingData->gunmsg[num].m_stopReason);
                /*
                if(readFile(QString("/usr/local/sanway/dbTest/charge.conf"), QString("ShowStopReason")).toInt() == 1)
                    stopReasonValue += QString::number(chargingData->gunmsg[focus_gun_-1].m_stopReason,16).toUpper();
                */
                stopReasonValue = QString::number(detailReason, 16);
                ui->labelStopReason->setText(stopReasonValue);
                break;
            case 6:
                //stopReasonValue = tr("BMS故障-");
                stopReasonValue = QString::number(detailReason, 16);
                ui->labelStopReason->setText(stopReasonValue);
                break;
            case 7:
                ui->labelStopReason->setText(tr("急停按钮停止"));
                break;
            case 8:
                ui->labelStopReason->setText(tr("验证码停止"));
                break;
            case 9:
                ui->labelStopReason->setText(tr("停电结束"));
                break;
            case 10:
                ui->labelStopReason->setText(tr("充电电量到自动停止"));
                break;
            case 11:
                ui->labelStopReason->setText(tr("充电金额到自动停止"));
                break;
            case 12:
                ui->labelStopReason->setText(tr("充电时间到自动停止"));
                break;
            case 13:
                ui->labelStopReason->setText(tr("拔枪结束"));
                break;
            case 255:
                ui->labelStopReason->setText(tr("其他"));
                break;
        }
    }
    else if(card->errcode != 0)
    {
        //读卡器故障
        if(chargingData->cmd3[focus_gun_ - 1].state==0)
            ui->labelStopReason->setText(QString(tr("读卡器故障")));
    }
}

int SettleAccount::fulling_Info(int GUN_NO)
{
    if ((chargingData->gunmsg[GUN_NO].u2.dc.MX[2] > 100)
        || (chargingData->gunmsg[GUN_NO].cost_elec > 100000)
        || (chargingData->gunmsg[GUN_NO].curkWh > 100000))
    {
        return -1;
    }

    St[GUN_NO].hour = chargingData->gunmsg[GUN_NO].charge_duration/3600;
    St[GUN_NO].min = (chargingData->gunmsg[GUN_NO].charge_duration % 3600)/60;
    St[GUN_NO].second = chargingData->gunmsg[GUN_NO].charge_duration % 60;
    St[GUN_NO].SOC = chargingData->gunmsg[GUN_NO].u2.dc.MX[2];
    St[GUN_NO].chargemoney = chargingData->gunmsg[GUN_NO].cost_elec;
    St[GUN_NO].chargepower = chargingData->gunmsg[GUN_NO].curkWh;
    St[GUN_NO].servermoney = chargingData->gunmsg[GUN_NO].cost_server;
    St[GUN_NO].allmoney    = chargingData->gunmsg[GUN_NO].curcostmoney;
    St[GUN_NO].stopreason  = chargingData->gunmsg[GUN_NO].stopreason;
    St[GUN_NO].stopDetailReason = chargingData->gunmsg[GUN_NO].m_stopReason;
    qDebug() << "charge duration: " << chargingData->gunmsg[GUN_NO].charge_duration << "s";

    return 0;
}

void SettleAccount::updateSettleAccount(int GUN_NO)//收到停止信号启动这个槽函数,开启更新结算信息定时器
{
    UpDateSettle[GUN_NO]->start(800);
}

SettleAccount::~SettleAccount()
{
    delete ui;
}

void SettleAccount::countdownOver()
{
    /*待完成*/
    CountDownTime* sender_ = static_cast<CountDownTime*>(sender());
    for(int i=0; i<GUN_AMOUNT; i++)
    {
        if(sender_ == countdown[i])//结算界面倒计时结束
        {
            GUN_STATE[i].Page = returnChargeGunPage;
            emit returnChargeGunCountdownStart(i);
        }
    }
}

void SettleAccount::on_buttonOk_clicked()
{
    /*待完成*/
    GUN_STATE[focus_gun_ - 1].Page = returnChargeGunPage;
    countdown[focus_gun_ - 1]->CountDownStop();
}

void SettleAccount::on_pushButton_clicked()
{
    Init_Focus();
}
